﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data;

namespace VechilePost
{
    public class SqlHelper
    {
        public static string conStr;


        public static bool Update(string sql)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
        }

        public static bool Exists(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {

                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();

                    int cmdresult;
                    object obj = cmd.ExecuteScalar();
                    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                    {
                        cmdresult = 0;
                    }
                    else
                    {
                        cmdresult = int.Parse(obj.ToString());
                    }
                    if (cmdresult == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }

                }
            }
        }

        public static DataTable GetTable(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection connection = new SqlConnection(conStr))
            {
                try
                {
                    DataSet ds = new DataSet();
                    using (SqlCommand cmd = new SqlCommand(sql, connection))
                    {
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        connection.Open();
                        SqlDataAdapter command = new SqlDataAdapter(cmd);
                        command.Fill(ds);
                        return ds.Tables[0];
                    }

                }
                catch
                {
                    throw;
                }

            }
        }

    }
}
